<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body>
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="SPU" prop="spu">
                    <el-input v-model.trim="queryParams.spu" placeholder="请输入SPU" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item>
            <el-form-item label="">
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        </div>
        <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({})">添加</el-button>
                </el-col>
            </el-row>
        <el-table v-loading="loading" :data="tableData" border script>
            <el-table-column label="ID" prop="id" min-width="40"></el-table-column>
            <el-table-column label="供应商名称" prop="supply_name" min-width="100" show-overflow-tooltip sortable>
            </el-table-column>
            <el-table-column label="供应商链接" prop="supply_link" min-width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="SPU" prop="spu" min-width="60" show-overflow-tooltip></el-table-column>
            <el-table-column label="创建时间" prop="created_time" min-width="100"></el-table-column>
            <el-table-column label="备注" prop="remark" min-width="100" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" min-width="100">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="showDetailDialog(scope.row)">编辑</el-button>
                    <el-button type="text" icon="el-icon-edit" @click="deleteItem(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
    </el-dialog>
</template>

<script>
import { querySupplySpuList, supListDelete } from '@/api/supply'
import { STATUS_LIST, IS_HEAD_LIST } from '../constant'
export default {
    name: 'DetailDialogspuinfo',
    components: {},
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            showSearch: true,
            // 查询参数
            queryParams: {
                spu: undefined,
            },
            loading: false,
            save: false,
            // 表格数据
            tableData: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            total: 0,
            rules: {
                supply_name: [{ required: true, message: '供应商名称不能为空', trigger: ['blur', 'change'] }],
                supply_link: [{ required: true, message: '供应商链接不能为空', trigger: ['blur', 'change'] }],
                // SPU: [{ required: true, message: 'SPU不能为空', trigger: ['blur', 'change'] }],
            },
            dict: {
                STATUS_LIST,
                IS_HEAD_LIST
            },
            detailDialog: {
                visible: false,
                data: {}
            }
        }
    },
    computed: {
        title() {
            return `查看供应商明细`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.getList()
                }
            },
            immediate: true,
            deep: true,
        },
    },
    mounted() {
        // this.getList()
    },
    methods: {
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },
        getList() {
            this.loading = true
            const params = { ...this.pageInfo, supply_id: this.params.id, spu: this.queryParams.spu }
            querySupplySpuList(params)
                .then(res => {
                    this.tableData = res.data.list
                    this.total = res.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },
        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            // if(this.detailDialog?.data?.supply_id) {
                this.detailDialog.data.supply_id = this.params.id
                this.detailDialog.data.supply_name = this.params.supply_name
                console.log('1111', this.detailDialog.data);
                
            // }
        },
        deleteItem(row){
            this.$confirm('确认要删除该供应商吗?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(()=>{
                supListDelete({id: row.id}).then((res)=>{
                    this.$message.success(res.msg)
                    this.getList()
                })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
}
</script>

<style>
.form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
}
.form-class.el-form .el-form-item .el-input {
    width: 400px;
}
</style>
